# -*- coding: utf-8 -*-

"""
组合模式
模式特点：将对象组合成成树形结构以表示“部分-整体”的层次结构

程序实例：公司人员的组织结构

代码特点：无
"""


class Component(object):
    def __init__(self, name):
        self.m_strName = name

    def add(self, com):
        pass

    def display(self, n_depth):
        pass


class Leaf(Component):

    def add(self, com):
        print "leaf can't add"

    def display(self, n_depth):
        strtemp = ""
        for i in range(n_depth):
            strtemp += "-"
        strtemp += self.m_strName
        print strtemp


class Composite(Component):

    def __init__(self, name):
        self.m_strName = name
        self.c = []

    def add(self, com):
        self.c.append(com)

    def display(self, n_depth):
        strtemp = ""
        for i in range(n_depth):
            strtemp += "-"
        strtemp += self.m_strName
        print strtemp
        for com in self.c:
            com.display(n_depth+2)


if __name__ == "__main__":
    p = Composite("Wong")
    p.add(Leaf("Lee"))
    p.add(Leaf("Zhao"))

    p1 = Composite("Wu")
    p1.add(Leaf("San"))

    p.add(p1)
    p.display(1)
